package org.third.integration.suite.idm.hpsso;

import com.hp.sw.bto.ast.security.lwcrypto.LWCrypto;
import com.hp.sw.bto.ast.security.lwcrypto.LWCryptoConstants;
import com.hp.sw.bto.ast.security.lwcrypto.LWCryptoFactory;
import com.hp.sw.bto.ast.security.lwcrypto.LWMAC;

import java.util.Properties;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.third.security.crypto.CryptoAlgorithm;

public class IdmLwssoTest {

	static final Logger logger = LoggerFactory.getLogger(IdmLwssoTest.class);

	@Test
	public String generateAesKey(int keySize) throws Exception {
		javax.crypto.KeyGenerator kgen = javax.crypto.KeyGenerator.getInstance(CryptoAlgorithm.AES.getName());
		kgen.init(keySize, new java.security.SecureRandom());
		javax.crypto.SecretKey secretKey = kgen.generateKey();
		byte[] enCodeFormat = secretKey.getEncoded();
		return new String(java.util.Base64.getUrlEncoder().encode(enCodeFormat));
	}

	public static void main(String[] args) throws Exception {
		IdmLwssoTest t = new IdmLwssoTest();
		// String aesKey = "";
		// aesKey = t.generateAesKey(256);
		// System.out.println(aesKey);

		// testJce();
		testLw();
	}

	private static void testLw() throws Exception {
		Properties cryptoProperties = new Properties();
//		cryptoSource="jce" cipherType="symmetricBlockCipher"
//	            engineName="AES" paddingModeName="CBC" keySize="256"
//	            pbeDigestAlgorithm="SHA1" encodingMode="Base64Url"
//	            jcePBECryptoAlgorithm="AES" jcePbeMacAlgorithmName="AES"
//	            macType="hmac"
//	            macAlgorithmName="SHA1" directKeyEncoded="true" directKeyEncoding="Base64"
//	            algorithmPaddingName="PKCS5Padding"
//	            pbeCount="20" macKeySize="256" macPbeCount="20"
//	            initString="YbyIQOrgx3oC4NVil_4iyeQaUrLLTGD0mq_vOZS0rj0="
		String engineName="AES";
		String algorithmMode;
		String algorithmPadding="CBC";
		String encodingMode="Base64Url";
//		LWCryptoConstants.SHA_256_DIGEST = "SHA256";
//	    LWCryptoConstants.SHA_1_DIGEST = "SHA1";
//	    LWCryptoConstants.MD5_DIGEST = "MD5";
		String macHashName="";  
		String macType="hmac";//LWCryptoConstants.HMAC_MAC
		String initData="YbyIQOrgx3oC4NVil_4iyeQaUrLLTGD0mq_vOZS0rj0";
		String pbeDigestAlgorithm="SHA1";
		String initString = "YbyIQOrgx3oC4NVil_4iyeQaUrLLTGD0mq_vOZS0rj0";
		int keySize=256;
		int pbeCount=20;
		boolean forEncryption;
		boolean jcePBECompatibilityMode;

		cryptoProperties.put(LWCryptoConstants.AES_ENGINE, "");
		// hpSsoConfiguration.getCryptoProperties();
		// String initString = hpSsoConfiguration.getInitString();
		// GlobalCrypto cryptoConfig =
		// hpSsoConfiguration.getGlobal().getLwsso().getCrypto();
		// String checkIntegrity = cryptoConfig.getCheckIntegrity();
		// Properties macProperties = cryptoConfig.getMacCryptoProperties();
		Properties macProperties = new Properties();
		
		try {
			logger.debug("cryptoProperties [" + cryptoProperties + "].");
			logger.debug("Get encryptor from LWCryptoFactory");

			LWCrypto encryptor = LWCryptoFactory.generatePBECipher(cryptoProperties, initString, true);
			if (logger.isDebugEnabled()) {
				logger.debug("Success to get encryptor [" + encryptor + "] from LWCryptoFactory");
			}
			// String encryptToken = encryptor.encrypt(tokenValue);
			logger.debug("Get decryptor from LWCryptoFactory");

			LWCrypto decryptor = LWCryptoFactory.generatePBECipher(cryptoProperties, initString, false);
			if (logger.isDebugEnabled()) {
				logger.debug("Success to get decryptor [" + decryptor + "] from LWCryptoFactory");
			}
			// decryptedText = decryptor.decrypt(cookieValue);
			logger.debug("macProperties [" + macProperties + "].");
			logger.debug("Get mac from LWCryptoFactory");
			LWMAC mac = LWCryptoFactory.generateMAC(macProperties, initString);
			if (logger.isDebugEnabled()) {
				logger.debug("Success to get mac [" + mac + "] from LWCryptoFactory");
			}

		} catch (Exception e) {
			String errorString = "Cannot create encryptor object.";
			logger.error(errorString, e);
		}
		// ConfigurationManagementUtils.init(IdmLwssoTest.class.getResource("hpssoConfig.lw.xml").getFile());
		// LWCrypto idmEncrypto = HpSsoCryptoUtils.getEncryptor();
		// String text1 = "asdfasfd";
		// String
		// encryptedText="Fwhlo_3P6tDtUn_BlQeKsOnZcDqGKfqYefsl8CBR25ygA15SoLIXLGEkf5zi2jhr6896edBZru2mgwCJLC3Zg6QMSbthn_j0eXgT3SPYAUE9jXTY2Lz8njdVS_8kEWt1wKbE0RhpihH8DzghF28ZPBfTtuRZj0LFYmeReUMetUc";
		//// encryptedText = idmEncrypto.encrypt(text1);
		//// System.out.println(encryptedText);
		//
		// LWCrypto idmDecrypto = HpSsoCryptoUtils.getDecryptor();

	}

	private static void testJce() throws Exception {

		// ConfigurationManagementUtils.init(IdmLwssoTest.class.getResource("hpssoConfig.jce.xml").getFile());
		// LWCrypto idmEncrypto = HpSsoCryptoUtils.getEncryptor();
		// String text1 = "asdfasfd";
		// String encryptedText = idmEncrypto.encrypt(text1);
		// System.out.println(encryptedText);
		//
		// LWCrypto idmDecrypto = HpSsoCryptoUtils.getDecryptor();
		// System.out.println(idmDecrypto.decrypt(encryptedText));
	}
}
